package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.StorageRecord;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.StorageRecordMapper;
import com.ktjiaoyu.service.StorageRecordService;
import com.ktjiaoyu.service.SupplierService;
import com.ktjiaoyu.service.SysRoleService;
import com.ktjiaoyu.utils.Page;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("storageRecordService")
public class StorageRecordServiceImpl implements StorageRecordService {
    private static final Logger looger = LoggerFactory.getLogger(SupplierServiceImpl.class);

    @Resource
    private StorageRecordMapper storageRecordMapper;


    @Override
    public Page<StorageRecord> getPage(StorageRecord entity, Integer pageNo, Integer pageSize) {
        Page<StorageRecord> page = null;
        try {
            page = new Page<StorageRecord>();
            page.setPageSize(pageSize);
//            查询总数，计算总页数
            Long count = storageRecordMapper.count(entity);
            page.setTotalCount(count.intValue());
            page.setPageNo(pageNo);
            //            查询分页数据
            List<StorageRecord> data = storageRecordMapper.getPage(entity, page.offset(), page.getPageSize());
            page.setData(data);
        } catch (Exception e) {
            looger.error("执行StorageRecordServiceImpl.getPage(StorageRecord,Integer,Integer)方法出现异常.", e);
            throw e;
        }
        return page;
    }

    @Override
    public boolean saveOrUpdate(StorageRecord entity) {
        try {
            if (entity.getId() == null) {
//                无主键则新增
                return storageRecordMapper.addStorage(entity) > 0;
            } else {
//                有主键则修改
                return storageRecordMapper.updateStorage(entity) > 0;
            }
        } catch (Exception e) {
            looger.error("执行 StorageServiceImpl.saveOrUpdate(Storage)方法出现异常，", e);
            throw e;
        }
    }

    @Override
    public StorageRecord getById(Long id) {
        try {
            return storageRecordMapper.getById(id);
        } catch (Exception e) {
            looger.error("执行StorageRecordServiceImpl.getById(Integer)方法出现异常.", e);
            throw e;
        }
    }

    @Override
    public StorageRecord findBySrCode(String srCode) throws Exception {
        try {
            return storageRecordMapper.getBySrCode(srCode);
        } catch (Exception e) {
            looger.error("执行 StorageServiceImpl" + ".findBySrCode(String)方法出现异常.", e);
            throw e;
        }
    }

    @Override
    public int deleteStorage(Long id) {
        try {
            return storageRecordMapper.deleteStorage(id) ;
        } catch (Exception e) {
            looger.error("执行StorageRecordServiceImpl.deleteStorage(Integer)方法出现异常.", e);
            throw e;
        }
    }
}
